package ca.kooki.codesnippet;

import ca.kooki.codesnippet.core.Action;
import ca.kooki.codesnippet.core.User;

public class CodeSnippetRepositoryHelper {

  
  //```````````````````````
  // MEMBER VARIABLES
  //```````````````````````

  private User _user;
  
  //```````````````````````
  // CONSTRUCTOR
  //```````````````````````

  public CodeSnippetRepositoryHelper()
  {
    init();
  }
  
  private void init()
  {
    _user = null;
  }

  
  //```````````````````````
  // INTERFACE
  //```````````````````````
  
  public User getUser()
  {
    return _user == null ? User.ANNONYMOUS : _user;
  }
  
  public void setUser(User user)
  {
    _user = user == null || !user.isOfficial() ? null : user;
  }
  
  public Action actionForNewUser(User user)
  {
    Action action = new Action("new user","created " + user.getName());
    return prepareAction(action);
  }
  
  public Action actionForNewCodeSnippet(CodeSnippet snippet)
  {
    Action action = new Action("new code snippet","added " + snippet.getTitle());
    return prepareAction(action);
  }
  
  public Action actionForNewTag(Tag tag)
  {
    Action action = null;
    if (tag.isOfficial())
    {
      action = new Action("update tag","[" + tag.getId() + "] " + tag.getTitle());
    }
    else
    {
      action = new Action("new tag",tag.getTitle());
    }
    return prepareAction(action);
  }
  
  public Action actionForNewSeach(String query, int numberOfResults)
  {
    Action action = new Action("new search","found ["+ numberOfResults +"]: " + query);
    return prepareAction(action);
  }
  
  //```````````````````````
  // PRIVATE METHODS
  //```````````````````````
  
  private Action prepareAction(Action action)
  {
    action.setUser(getUser());
    return action;
  }
  
}
